package com.code.zjm.receiver.demo;

import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
    private Button btnSendBroadcast, btnRegBCR, btnUnRegBCR;
    private final MainBC mainBC = new MainBC();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btnSendBroadcast = (Button) findViewById(R.id.btnSendBroadcast);
        btnSendBroadcast.setOnClickListener(new View.OnClickListener() {//发送广播
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainBC.ACTION);
                intent.putExtra("text", "This is Broadcast");
                sendBroadcast(intent);
            }
        });
        btnRegBCR = (Button) findViewById(R.id.btnRegBCR);
        btnRegBCR.setOnClickListener(new View.OnClickListener() {//注册广播
            @Override
            public void onClick(View v) {
                registerReceiver(mainBC, new IntentFilter(MainBC.ACTION));
            }
        });
        btnUnRegBCR = (Button) findViewById(R.id.btnUnRegBCR);
        btnUnRegBCR.setOnClickListener(new View.OnClickListener() {//注销广播
            @Override
            public void onClick(View v) {
                unregisterReceiver(mainBC);
            }
        });
    }
}
